package cn.clq.ymcc.mq;

import cn.clq.ymcc.domain.MediaFile;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author clqlongbao
 * @version 1.0
 * @description: TODO
 * @date 2022/7/24 9:28
 */
@Component
public class MediaFileProducer {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public boolean sendMessage(MediaFile mediaFile){
        //为了方便消费者重新将message封装成MediaFile对象，如果这里直接传对象的话，消费者反序列化会很麻烦
        //所以这里先将mediaFile转换成json字符串再传递
        String jsonString = JSON.toJSONString(mediaFile);
        SendResult sendResult = rocketMQTemplate.syncSend("media-topic:media-tags", jsonString);
        SendStatus sendStatus = sendResult.getSendStatus();
        return sendStatus==SendStatus.SEND_OK;
    }
}
